package com.yssoft.controller.app.production;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yssoft.entity.production.TBatch;
import com.yssoft.service.production.BatchService;
import com.yssoft.utils.AppUtil;

/**
 * @author 戈超
 * @time 2018/5/09 上午 11:02
 * @生产批次控制器
 */
@CrossOrigin//解决跨域session问题
@Controller
@RequestMapping(value = "/app/productionBatch")
public class BatchController {
    private static Logger logger = Logger.getLogger(BatchService.class);

    @Resource
    private BatchService batchService;

    /**
     * 查询生产批次列表
     *
     * @param currentPage
     * @param pageSize
     * @param userId
     * @param code
     * @return
     * @author 戈超
     */
    @ResponseBody
    @RequestMapping(value = "queryBatchList", method = {RequestMethod.GET, RequestMethod.POST})
    public Object queryBatchList(Integer currentPage, Integer pageSize, String userId, String code, HttpServletRequest request) {
        String msg = "生产批次列表查询";
        try {
            List<Map<String, Object>> pageList = batchService.queryBatchList(currentPage, pageSize, userId, code, request);
            return AppUtil.returnListJson("1", msg + "成功!", pageList);
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnListJson("0", msg + "失败!", null);
        }
    }

    /**
     * 生产批次状态修改
     *
     * @param tBatch
     * @return
     * @author 戈超 测试
     */
    @ResponseBody
    @RequestMapping(value = "updateStat", method = {RequestMethod.GET, RequestMethod.POST})
    public Object updateStat(TBatch tBatch) {
        String msg = "生产批次状态修改";
        try {
            int result = batchService.updateStat(tBatch);
            if (result > 0) {
                return AppUtil.returnMapJson("1", msg + "成功!", null);
            } else {
                return AppUtil.returnMapJson("0", msg + "失败!", null);
            }
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnMapJson("0", msg + "失败!", null);
        }
    }

    /**
     * 选择产品分类查询
     *
     * @param type
     * @param request
     * @return
     * @author 戈超
     */
    @ResponseBody
    @RequestMapping(value = "queryProductClassifyList", method = {RequestMethod.GET, RequestMethod.POST})
    public Object queryProductClassifyList(String type, HttpServletRequest request) {
        String msg = "选择产品分类查询";
        try {
            List<Map<String, Object>> pageList = batchService.queryProductClassifyList(type, request);
            return AppUtil.returnListJson("1", msg + "成功!", pageList);
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnListJson("0", msg + "失败!", null);
        }
    }

    /**
     * 选择产品查询
     *
     * @param productName
     * @param request
     * @return
     * @author 戈超
     */
    @ResponseBody
    @RequestMapping(value = "queryProduct", method = {RequestMethod.GET, RequestMethod.POST})
    public Object queryProduct(Integer currentPage, Integer pageSize, String productName,
                               String type, String tPid, HttpServletRequest request) {
        String msg = "选择产品查询";
        try {
            if (type != null && !"".equals(type)) {
                List<Map<String, Object>> pageList = batchService.queryProduct(currentPage, pageSize, productName, type, tPid, request);
                return AppUtil.returnListJson("1", msg + "成功!", pageList);
            } else {
                return AppUtil.returnListJson("0", msg + "失败!刷新页面重试", null);
            }
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnListJson("0", msg + "失败!", null);
        }
    }

    /**
     * 生产批次创建
     *
     * @param tBatch
     * @param request
     * @return
     * @author 戈超
     */
    @ResponseBody
    @RequestMapping(value = "batchSave", method = {RequestMethod.GET, RequestMethod.POST})
    public Object batchSave(TBatch tBatch, HttpServletRequest request) {
        String msg = "生产批次创建";
        try {
            List<TBatch> list = batchService.queryBatchBySourceId(tBatch, request);
            //需要验证产品是否关联物料
            boolean isMaterial = batchService.isSerGoodMaterial(tBatch.gettSourceId());
            if (list.size() > 0) {
                return AppUtil.returnMapJson("0", "该产品批次号已存在", null);
            }else if (!isMaterial){
                return AppUtil.returnMapJson("0", "该产品没有关联物料，请先关联物料", null);
            }
            else {

                int result = batchService.insertBatch(tBatch, request);
                if (result > 0) {
                    return AppUtil.returnMapJson("1", msg + "成功!", null);
                } else {
                    return AppUtil.returnMapJson("0", msg + "失败!", null);
                }
            }
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnMapJson("0", msg + "失败!", null);
        }
    }

    /**
     * 根据生产批次id查询
     *
     * @param type
     * @param id
     * @param request
     * @return
     * @author 戈超
     */
    @ResponseBody
    @RequestMapping(value = "queryBatchById", method = {RequestMethod.GET, RequestMethod.POST})
    public Object queryBatchById(Integer id, HttpServletRequest request) {
        String msg = "根据生产批次id查询";
        try {
            Map<String, Object> map = batchService.queryBatchById(id, request);
            return AppUtil.returnMapJson("1", msg + "成功!", map);
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnMapJson("0", msg + "失败!", null);
        }
    }

    /**
     * 生产批次编辑
     *
     * @param tBatch
     * @return
     * @author 戈超
     */
    @ResponseBody
    @RequestMapping(value = "batchEdit", method = {RequestMethod.GET, RequestMethod.POST})
    public Object batchEdit(TBatch tBatch, HttpServletRequest request) {
        String msg = "生产批次编辑";
        try {
            List<TBatch> list = batchService.queryBatchBySourceId(tBatch, request);
            if (list.size() > 0) {
                return AppUtil.returnMapJson("0", "该产品批次号已存在", null);
            } else {
                int result = batchService.batchEdit(tBatch);
                if (result > 0) {
                    return AppUtil.returnMapJson("1", msg + "成功!", null);
                } else {
                    return AppUtil.returnMapJson("0", msg + "失败!", null);
                }
            }
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnMapJson("0", msg + "失败!", null);
        }
    }

    /**
     * 生产批次删除
     *
     * @param id
     * @return
     * @author 戈超
     */
    @ResponseBody
    @RequestMapping(value = "batchDelete", method = {RequestMethod.GET, RequestMethod.POST})
    public Object batchDelete(Integer id) {
        String msg = "生产批次删除";
        try {
            int result = batchService.batchDelete(id);
            if (result > 0) {
                return AppUtil.returnMapJson("1", msg + "成功!", null);
            } else {
                return AppUtil.returnMapJson("0", msg + "失败!", null);
            }
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnMapJson("0", msg + "失败!", null);
        }
    }

    /**
     * 生产批次选择列表查询
     *
     * @param currentPage
     * @param pageSize
     * @param code
     * @param request
     * @return
     * @author 戈超
     */
    @ResponseBody
    @RequestMapping(value = "queryBatchSelectList", method = {RequestMethod.GET, RequestMethod.POST})
    public Object queryBatchSelectList(Integer currentPage, Integer pageSize, String code, HttpServletRequest request) {
        String msg = "生产批次选择列表查询";
        try {
            List<Map<String, Object>> pageList = batchService.queryBatchSelectList(currentPage, pageSize, code, request);
            return AppUtil.returnListJson("1", msg + "成功!", pageList);
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnListJson("0", msg + "失败!", null);
        }
    }

    /**
     * 查询生产批次列表-成品个体溯源的-标签申请用
     *
     * @param currentPage
     * @param pageSize
     * @param userId
     * @param code
     * @return
     * @author 王永宁
     */
    @ResponseBody
    @RequestMapping(value = "queryBatchListForLabel", method = {RequestMethod.GET, RequestMethod.POST})
    public Object queryBatchListForLabel(Integer currentPage, Integer pageSize, String userId, String code, HttpServletRequest request) {
        String msg = "生产批次列表查询";
        try {
            List<Map<String, Object>> pageList = batchService.queryBatchListForLabel(currentPage, pageSize, userId, code, request);
            return AppUtil.returnListJson("1", msg + "成功!", pageList);
        } catch (Exception e) {
            logger.info(msg + "失败：" + e.getMessage());
            return AppUtil.returnListJson("0", msg + "失败!", null);
        }
    }

    @ResponseBody
    @RequestMapping(value = "batchCodeCheck", method = {RequestMethod.GET, RequestMethod.POST})
    public Object batchCodeCheck(String batchCode, Integer sourceId, String sourceType, HttpServletRequest request) {
        try {
            List<TBatch> bList = batchService.selectBatchByCode(batchCode, sourceId, sourceType);
            if (bList != null && bList.size() > 0) {
                return AppUtil.returnListJson("0", "该批次码已存在", null);
            } else {
                return AppUtil.returnListJson("1", "有效批次码", null);
            }
        } catch (Exception e) {
            logger.info("批次码校验失败：" + e.getMessage());
            return AppUtil.returnListJson("0", "批次码校验失败!", null);
        }
    }
}
